# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=ruff
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.11.11
pkgrel=1
pkgdesc="An extremely fast Python linter, written in Rust (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://astral.sh/ruff'
msys2_repository_url='https://github.com/astral-sh/ruff'
license=('spdx:MIT')
msys2_references=(
  'archlinux: ruff'
  'anitya: 370829'
  'gentoo: dev-util/ruff'
  'purl: pkg:pypi/ruff'
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-python-maturin"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  'git'
)
options=('!strip')
source=("git+${msys2_repository_url}.git#tag=${pkgver}")
sha256sums=('faa8bb71c785799f3cb024a0821a21c6118d811d7c456a34ef95f4b52e3d057c')

prepare() {
  cd "${_realname}"

  # otherwise cargo installs msvc toolchain from rustup
  rm rust-toolchain.toml

  cargo fetch --locked --config='net.git-fetch-with-cli=true'
}

build() {
  cd "${_realname}"

  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  maturin build --release --strip --frozen --all-features
}

check() {
  cd "${_realname}"

  export WINAPI_NO_BUNDLED_LIBRARIES=1
  cargo test --release --frozen --all-features
}

package_ruff() {
  install -Dm755 "${_realname}/target/release/ruff" "${pkgdir}${MINGW_PREFIX}/bin/ruff"
  install -Dm644 "${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  local _complete="${_realname}/target/release/ruff generate-shell-completion"
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/${_realname}"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/${_realname}.fish"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_${_realname}"
}

package_python-ruff() {
  pkgdesc+=" (Python module)"
  depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-ruff")

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" "${_realname}"/target/wheels/*.whl

  # split ruff binary
  rm -rf "${pkgdir}${MINGW_PREFIX}/bin/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
